package com.team7.happycommunity.businessservice.service.impl;

import com.team7.happycommunity.businessservice.entity.BusinessImage;
import com.team7.happycommunity.businessservice.dao.BusinessImageDao;
import com.team7.happycommunity.businessservice.entity.ServiceImg;
import com.team7.happycommunity.businessservice.service.BusinessImageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (BusinessImage)表服务实现类
 *
 * @author makejava
 * @since 2020-03-25 20:07:28
 */
@Service("businessImageService")
public class BusinessImageServiceImpl implements BusinessImageService {
    @Resource
    private BusinessImageDao businessImageDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BusinessImage queryById(Integer id) {
        return this.businessImageDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<BusinessImage> queryAllByLimit(int offset, int limit) {
        return this.businessImageDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param businessImage 实例对象
     * @return 实例对象
     */
    @Override
    public BusinessImage insert(BusinessImage businessImage) {
        this.businessImageDao.insert(businessImage);
        return businessImage;
    }

    /**
     * 修改数据
     *
     * @param businessImage 实例对象
     * @return 实例对象
     */
    @Override
    public BusinessImage update(BusinessImage businessImage) {
        this.businessImageDao.update(businessImage);
        return this.queryById(businessImage.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.businessImageDao.deleteById(id) > 0;
    }

    @Override
    public void uploadHead(String url, Integer userId){
        //查询该用户是否有头像
        //1.用户没有头像，插入数据
        BusinessImage img=new BusinessImage();
        img.setBusinessInfoId(userId);
        img.setType(0);
        List<BusinessImage> businessImages = businessImageDao.queryAll(img);
        if(businessImages==null||businessImages.size()==0){
            //上传头像
            img.setUrl(url);
            businessImageDao.insert(img);
            return;
        }
        //2.用户已有头像，修改数据
        img.setUrl(url);
        img.setId(businessImages.get(0).getId());
        businessImageDao.update(img);
    }
}